package com.yc.snack.biz.impl;

import com.yc.snack.biz.IProductInfoBiz;
import com.yc.snack.dao.IProductInfoDao;
import com.yc.snack.dao.impl.ProductInfoDaoImpl;
import com.yc.snack.entity.ProductInfo;
import com.yc.snack.util.StringUtil;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: HuYang
 * @Date: 2022/11/18 - 11 - 18 - 21:04
 * @Description: com.yc.snack.biz.impl
 * @version: 1.0
 */
public class ProductInfoBizImpl implements IProductInfoBiz {
    @Override
    public int add(ProductInfo productInfo) {
        if(StringUtil.checkNull(productInfo.getGname(), productInfo.getPrice(), productInfo.getTno(), productInfo.getPics())) {
            return -1;
        }
        IProductInfoDao productInfoDao = new ProductInfoDaoImpl();
        return productInfoDao.add(productInfo);
    }

    @Override
    public List<ProductInfo> finds(int page, int rows) {
        IProductInfoDao productInfoDao = new ProductInfoDaoImpl();
        return productInfoDao.finds(page, rows);
    }

    @Override
    public Map<String, Object> findByFirst(int page, int rows) {
        IProductInfoDao productInfoDao = new ProductInfoDaoImpl();
        List<ProductInfo> list = productInfoDao.finds(page, rows);
        if(list == null || list.isEmpty()) {
            return Collections.emptyMap();  // 返回空集合，不是空对象
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", productInfoDao.total());
        map.put("rows", list);
        return map;
    }
}
